package com.example.smartcity.ui.Adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.example.smartcity.R;
import com.example.smartcity.ui.Bean.StopItem;

import java.util.List;

public class StopCarAdapter extends ArrayAdapter<StopItem> {
    private int resource;
    public StopCarAdapter(@NonNull Context context, int resource, @NonNull List<StopItem> objects) {
        super(context, resource, objects);
        this.resource = resource;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        StopItem item = getItem(position);
        View view = null;
        ViewHolder viewHolder = null;
        if (convertView==null){
            view = LayoutInflater.from(getContext()).inflate(resource,parent,false);
            viewHolder = new ViewHolder();
            viewHolder.TV_address = view.findViewById(R.id.TV_address);
            viewHolder.TV_distance = view.findViewById(R.id.TV_distance);
            viewHolder.TV_rates = view.findViewById(R.id.TV_rates);
            viewHolder.TV_vacancy = view.findViewById(R.id.TV_vacancy);
            viewHolder.TV_parkName = view.findViewById(R.id.TV_parkName);
            view.setTag(viewHolder);
        }else{
            view = convertView;
            viewHolder = (ViewHolder) view.getTag();
        }
        viewHolder.TV_parkName.setText(item.getParkName());
        viewHolder.TV_vacancy.setText("空位"+item.getVacancy()+"个");
        viewHolder.TV_rates.setText("停车费"+item.getRates()+"元/小时");
        viewHolder.TV_distance.setText(item.getDistance()+"公里");
        viewHolder.TV_address.setText(item.getAddress());
        return view;

    }
    class ViewHolder{
        TextView TV_parkName;
        TextView TV_distance;
        TextView TV_vacancy;
        TextView TV_rates;
        TextView TV_address;
    }
}
